#!/bin/sh
echo 91 > /sys/class/gpio/export
fac=$(cat /sys/class/gpio/gpio91/value)
if [ $fac = 0 ]
then
        rm -rf /app/device
        cp -rf /app/deviceback /app/device
fi
 chmod +x /app/listen.sh
 /app/listen.sh &
 echo 137 > /sys/class/gpio/export
 echo "out" > /sys/class/gpio/gpio137/direction
 echo 8 > /sys/class/gpio/export
 echo "out" > /sys/class/gpio/gpio8/direction
 echo 9 > /sys/class/gpio/export
 echo "out" > /sys/class/gpio/gpio9/direction
 echo 0 > /sys/class/gpio/export
 echo "out" > /sys/class/gpio/gpio0/direction
mount /dev/mmcblk0 /media 
if [ -e /app/tw_em1 ]; then
 	mv /app/tw_em1 /app/tw_em
 	chmod 777 /app/tw_em
 fi
/app/help &
spawn-fcgi -a 127.0.0.1 -p 9876 -f /app/tw_em -P /tmp/spwn.pid


result=`ifconfig eth1|grep -w 'inet'|cut -d ":" -f2|cut -d "." -f1,2,3`
end=".1"
gwip=$result$end
echo $gwip
route del -net 0.0.0.0/0 dev eth0
route add -net 0.0.0.0/0 gw $gwip dev eth1

ipadd=`ifconfig eth1|grep -w 'inet'|cut -d ":" -f2|cut -d " " -f1`
echo 'nameserver 8.8.8.8' >> /etc/resolv.conf
echo '1' >> /proc/sys/net/ipv4/ip_forward
iptables -A POSTROUTING -t nat -s 192.168.66.0/24  -o eth1 -j SNAT --to-source $ipadd